/*
 * @Description: 
 * @Author: charles
 * @Date: 2021-10-21 15:21:28
 * @LastEditors: charles
 * @LastEditTime: 2021-10-21 16:22:58
 */
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/home'
  }, {
    path: '/register',
    component: () => import(/* webpackChunkName: "register" */ '../pages/Register.vue')

  }, {
    path: '/login',
    component: () => import(/* webpackChunkName: "login" */ '../pages/Login.vue')
  }, {
    path: '/home',
    component: () => import(/* webpackChunkName: "home" */ '../pages/Home.vue'),
    children: [{
      path: 'index',
      component: () => import(/* webpackChunkName: "home_index" */ '../pages/home/Index.vue'),
    },{
      path: 'orders',
      component: () => import(/* webpackChunkName: "home_orders" */ '../pages/home/Orders.vue'),
    },{
      path: 'me',
      component: () => import(/* webpackChunkName: "home_me" */ '../pages/home/Me.vue'),
    }]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next)=>{
  // 白名单
  let paths = ["/login","/register"]
  if(paths.indexOf(to.path)!=-1){
    next();
  }
  // 验证token
  let token = localStorage.getItem('token');
  if(!token){
    next('/login')
  } else {
    next();
  }
})

export default router
